package com.dk.service.impl;


import com.dk.mapper.MenuMapper;
import com.dk.mapper.PositionMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PositionMenuMapper positionMenuMapper;

    @Override
    public List<Map<String, Object>> getMenuInfo(Integer playerAccount) {
        // 先判断redis中是否存在
        String key = "permission";
        String field = "menu:info:" + playerAccount;
        //如果存在，直接返回
        HashOperations hashJedis = redisTemplate.opsForHash();
        boolean b = hashJedis.hasKey(key,field);
        if (b){
            List<Map<String, Object>> result = (List<Map<String, Object>>) hashJedis.get(key,field);
            return result;
        }else {
            //不存在，在数据库中查询并增加到redis
            List<Menu> list = menuMapper.selectById(playerAccount);
            List<Map<String, Object>> result = getMenuZtree(list, 0);
            hashJedis.put(key,field,result);
            return result;
        }
    }

    public List<Map<String, Object>> getMenuZtree(List<Menu> list, Integer id) {
        //定义一个容器,用于存放数据
        List<Map<String, Object>> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            //传递主键id 和 当前pid 相等
            Map<String, Object> map = null;
            Menu menu = list.get(i);
            if (id.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("id", menu.getMid());
                map.put("text", menu.getMname());
                map.put("url", menu.getUrl());
                //selectable指定列表树的节点是否可选择。设置为false将使节点展开，并且不能被选择。
                map.put("selectable", false);
                map.put("nodes", getMenuZtree(list, menu.getMid()));
            }

            //为什么map不等于null？如果map不等于null，说明map里面有值
            //然后获取nodes，里面的结构为List<Map<String,Object>>，因为是list，所有需要size判断，如果<=0,说明没有nodes，也就可以删除+号
            if (map != null) {
                List<Map<String, Object>> r = (List<Map<String, Object>>) map.get("nodes");
                if (r.size() <= 0) {
                    //删除+号的
                    map.remove("nodes");

                    map.put("selectable", true);
                }
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getZtreeMenu() {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Map<String, Object>> result = getMenuZtreeTo(list, 0);
        return result;
    }

    @Override
    public boolean add(Menu menu) {
        return menuMapper.insertSelective(menu) > 0;
    }

    @Override
    public Menu toUpdate(Integer menuId) {
        return menuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public boolean update(Menu menu) {
        return menuMapper.updateByPrimaryKeySelective(menu) > 0;
    }

    @Override
    public boolean deleteById(Integer menuId) {
        return menuMapper.deleteByPrimaryKey(menuId) > 0;
    }

    @Override
    public List<Map<String, Object>> getZtreeMenuChecked(Integer positionId) {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Integer> mids = positionMenuMapper.selectById(positionId);
        List<Map<String, Object>> result = getMenuZtreeToo(list, 0,mids);
        return result;
    }

    public List<Map<String,Object>> getMenuZtreeTo(List<Menu> list, Integer id){
        //定义一个容器,用于存放数据
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递主键id 和 当前pid 相等
            Menu menu = list.get(i);
            if(id.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("name",menu.getMname());
                map.put("id",menu.getMid());
                map.put("children",getMenuZtreeTo(list,menu.getMid()));
            }
            if (map != null){
                //删除 + 号的
                List<Map<String,Object>> r = (List<Map<String,Object>>)map.get("children");
                if (r.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }


    public List<Map<String,Object>> getMenuZtreeToo(List<Menu> list, Integer id,List<Integer> mids){
        //定义一个容器,用于存放数据
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递主键id 和 当前pid 相等
            Menu menu = list.get(i);
            if(id.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("name",menu.getMname());
                map.put("id",menu.getMid());
                map.put("children",getMenuZtreeToo(list,menu.getMid(),mids));
                if (mids != null && mids.size() > 0 && mids.contains(menu.getMid())){
                    map.put("checked",true);
                }
            }
            if (map != null){
                //删除 + 号的
                List<Map<String,Object>> r = (List<Map<String,Object>>)map.get("children");
                if (r.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }


}
